
layui.define(['index', 'layer', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var laypage = layui.laypage;
    var form = layui.form;
    var layer = layui.layer;
    var layedit = layui.layedit;

    laypage.render(zmnPage.laypageOptions(json));

    form.render();



    var hasDatas = json.hasDatas;
    if(hasDatas){
        var currentActivityId = json.currentActivityId;
        var actCityMiddleList = json.detail.actCityMiddleList;

        var oldData = {
            "activityId": currentActivityId,
        };
        if(actCityMiddleList){
            let province_checkbox_startWith = "provinceIds[";
            let city_checkbox_startWith = "cityIds[";
            let checkbox_endWith = "]";
            $.each(actCityMiddleList, function(i, item){
                var isAll = item.isAll;
                var cityId = item.cityId;
                var isNationwide = (isAll && (cityId == 0));
                if(isNationwide){
                    oldData['isNationwide'] = true;
                    //全国下面的所有省份和所有城市勾上
                    $(".act-city-all-checkbox").prop("checked", true);
                    return false;
                }
                if(item.level == 2){
                    var attr = province_checkbox_startWith + cityId + checkbox_endWith;
                    oldData[attr] = true;
                    //本省份下面的所有城市勾上
                    $('.act-city-all-checkbox[parentid=' + cityId +']').prop("checked", true);
                }else if(item.level == 3){
                    var attr = city_checkbox_startWith + cityId + checkbox_endWith;
                    oldData[attr] = true;
                }
            });
        }
        form.val("act-modify-city-form", oldData);
    }

    form.on('submit(act-modify-city-submit)', function (obj) {
        var currentActivityId = json.currentActivityId;
        var data = obj.field;
        data['activityId'] = currentActivityId;
        var isNationwide = data.isNationwide;
        console.info("isNationwide: " + isNationwide)
        //是否全国
        if(!isNationwide){
            let province_checkbox_startWith = "provinceIds[";
            let city_checkbox_startWith = "cityIds[";
            var provinceIds = new Array();
            var cityIds = new Array();
            $.each(data, function(attr, value){
                var flag = attr.startsWith(province_checkbox_startWith);
                if(flag){
                    provinceIds.push(value);
                    return true;
                }
                var flag = attr.startsWith(city_checkbox_startWith);
                if(flag){
                    cityIds.push(value);
                }
            });
            data['provinceIds'] = provinceIds;
            data['cityIds'] = cityIds;
            if(provinceIds.length == 0 && cityIds.length == 0){
                layer.open({
                    title: "提示",
                    content: '请先选择城市信息！',
                    yes: function(index, layero){
                        layer.close(index); //如果设定了yes回调，需进行手工关闭
                        return ;
                    }
                });
                return false;
            }
        }

        $("#act-modify-description-submit").addClass('layui-disabled').prop("disabled", true);
        $.ajax({
            type: "POST",
            url: "/activity/config/modify-city",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(data),
            success: function (res) {
                if (res.status == 200) {
                    // 不关闭
                    // zmnLayUtil.closeThisIframeMsg(parent, res.message, "zmn-activity-table");
                    layer.msg('保存成功!');
                }else{
                    layer.msg(res.message, {icon: 5, shift: 6});
                }
                $("#act-modify-description-submit").removeClass('layui-disabled').prop("disabled", false);
                return false;
            }
        });

        return false;
    });

    form.on('checkbox(act-city-checkbox-filter1)', function(data){
        // console.log(data.elem); //得到checkbox原始DOM对象
        // console.log(data.elem.checked); //是否被选中，true或者false
        // console.log(data.value); //复选框value值，也可以通过data.elem.value得到
        // console.log(data.othis); //得到美化后的DOM对象
        if(data.elem.checked){
            $(".act-city-all-checkbox").prop("checked", true);
        }else{
            $(".act-city-all-checkbox").prop("checked", false);
        }
        form.val("act-modify-city-form");
    });
    form.on('checkbox(act-city-checkbox-filter2)', function(data){
        var value = data.value;
        var selector = '.act-city-all-checkbox[parentid=' + value +']';
        if(data.elem.checked){
            $(selector).prop("checked", true);
            checkNationwide(data.elem);
        }else{
            $(selector).prop("checked", false);
            $('.act-nationwide-checkbox').prop("checked", false);
        }
        form.val("act-modify-city-form");
    });
    form.on('checkbox(act-city-checkbox-filter3)', function(data){
        var value = data.value;
        var parentid = $(data.elem).attr("parentid");
        var selector = '.act-city-all-checkbox[value=' + parentid +']';
        if(data.elem.checked){
            var checked = true;
            var siblings = $(data.elem)
                .parents('.layui-input-block-level2')
                .find('input.act-city-checkbox');
            siblings.each(function(){
                var item = this;
                if(!$(item).prop("checked")){
                        checked = false;
                        return false;
                    }
                });
            $(selector).prop("checked", checked);
            if(!checked){
                $('.act-nationwide-checkbox').prop("checked", false);
            }else{
                checkNationwide($(selector));
            }
        }else{
            $(selector).prop("checked", false);
            $('.act-nationwide-checkbox').prop("checked", false);
        }
        form.val("act-modify-city-form");
    });

    function checkNationwide(elem){
        var finds = $(elem).parents('.layui-field-box-modify-city')
            .find('.act-province-checkbox');
        var checked = true;
        finds.each(function(){
            var item = this;
            if(!$(item).prop("checked")){
                checked = false;
                return false;
            }
        });
        $('.act-nationwide-checkbox').prop("checked", checked);
    }


    $(document).on('click', '#act-modify-city-cancel', function () {
        zmnLayUtil.closeThisIframeMsg(parent, null, null);
    });
    //对外暴露的接口
    exports('zmn_plat_activity_modifyCity', {});
});

